package com.longlonggo.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lombok.extern.slf4j.Slf4j;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.longlonggo.util.RequestUtil;


/** 
 * 主拦截器，用以设置文件路径
 * @author 石马人山 me@longlonggo.com
 * @Created 2018年5月2日 下午5:54:29</p>
 * @version 100-000-000<br/>
 * @description <br/>
 */
@Slf4j
@Controller
public class BaseInterceptor implements HandlerInterceptor{

	private static String BASE = "base";

	private static String BASE_PATH = "basePath";
	
	public BaseInterceptor() {
		log.info("BaseInterceptor 已加载");
	}
	
	/**
	 * 所有action的拦截,主要拦截base与basepath
	 * 
	 * @param request
	 *            HttpServletRequest对象
	 * @param response
	 *            HttpServletResponse 对象
	 * @param handler
	 *            处理器
	 * @throws Exception
	 *             异常处理
	 * @return 处理后返回true
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		
		//获取根路径
		String contextPath = RequestUtil.getContextPath(request);
		String basePath = RequestUtil.getBasePath(request);
		String realPath = request.getServletContext().getRealPath("/");
		
		//---------保存员工登录实体信息
//		HttpSession session =  request.getSession();
//		EmployeeEntity employee = (EmployeeEntity) session.getAttribute(SessionEnum.EMPLOYEE_SESSION.toString());
//		request.setAttribute("employee", employee);
		//---------保存员工登录实体信息
		
		//项目地址
		request.setAttribute(BASE, contextPath);
		request.setAttribute(BASE_PATH, basePath);

		log.debug("----------base:" + contextPath);
		log.debug("----------basePath:" + basePath);
		log.debug("----------realPath:" + realPath);
		return true;
	}


	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub
		
	}
	
}
